<?php
/**
 * Created by PhpStorm.
 * User: Sky
 * Date: 2017/8/3
 * Time: 下午3:11
 */

namespace app\controllers;


use app\librarys\BaseController;
use app\models\Message;
use yii\data\Pagination;

class MessageController extends BaseController
{
    public function actionSystemMessageList()
    {
        $title = \Yii::$app->request->get('title');
        $pagination = new Pagination([
            'totalCount' => Message::getSystemMessageCount($title),
        ]);

        $items = Message::getSystemMessageList($pagination->page, $pagination->pageSize, $title);
        return $this->render('system-message-list', [
            'items' => $items,
            'pagination' => $pagination,
            'title' => $title
        ]);
    }

    /**
     * 根据id获取系统消息详情
     * @return string
     */
    public function actionSystemMessageDetailPartial()
    {
        $id = \Yii::$app->request->post('id', '');
        $item = Message::getById($id);
        return $this->renderPartial('system-message-detail-partial', [
            'id' => $id,
            'item' => $item
        ]);
    }

    /**
     * 保存系统消息
     * @return string
     */
    public function actionSystemMessageSaveHandler()
    {
        $id = \Yii::$app->request->post('id', '0');
        $title = \Yii::$app->request->post('title');
        $content = \Yii::$app->request->post('content');
        $result = Message::save($id, $title, $content);
        return $this->renderJson(null, $result ? 0 : 1, $result ? 'success' : 'fail');
    }

    public function actionFeedbackList()
    {
        $count = Message::getFeedbackCount();
        $pagination = new Pagination(['totalCount' => $count]);
        $items = Message::getFeedbackList($pagination->page, $pagination->pageSize);
        return $this->render('feedback-list',
            [
                'items' => $items,
                'pagination' => $pagination
            ]
        );
    }

    public function actionFeedbackDetailPartial()
    {
        $id = \Yii::$app->request->post('id');
        $item = Message::getFeedbackById($id);
        Message::setFeedbackRead($id);
        return $this->renderPartial('feedback-detail-partial', ['item' => $item]);
    }
}